requireJS(['constant', 'site', 'handlebars', 'public'], function (Constant, site, Handlebars, Public) {
    Constant.initHandlebars(Handlebars);
    Public.initProductSearch();
    Handlebars.registerHelper('if_eq', function (v1, v2, opts) {
        if (v1 == v2)
            return opts.fn(this);
        else
            return opts.inverse(this);
    });

    var id;

    var url = {
        listUrl: 'manager/selectPageDocService.do',
        delectDoc: 'manager/deleteDocByIdService.do',
        downloadDocByIdService: "manager/downloadDocByIdService.do",
        deleteDocByIdService:"manager/deleteDocByIdService.do",
        selectLoanMethodList: "manager/selectLoanMethodList.do"
    }
    // 部门查询
    site.send({
        url: "manager/departmentCascade.do",
        //async:false
        data: {parentId: 1},
        callback: function (data) {
            var list = JSON.parse(data)
            console.log(data)
            var source = $("#department_content_tpl").html();
            var template = Handlebars.compile(source);
            var html = template(list);
            $('#department_content').html(html);
        }
    })

    //渲染搜索框的贷款方式
    site.send({
        url: url.selectLoanMethodList,
        callback: function (data) {
            data = JSON.parse(data);
            if (data.type == '1') {
                var source = $("#loanMethod_list_tpl").html();
                var template = Handlebars.compile(source);
                var list = data
                var html = template(list)
                $('.loanWay_select').append(html);
            }
        }
    })


    var docListData = {
        pageNum: 1
    }

// 列表查询
    function docList() {
        site.send({
            url: url.listUrl,
            //async:false
            data: docListData,
            callback: function (data) {
                var list = JSON.parse(data)
                if (list.type == '1') {
                    var source = $("#b_content_tpl").html();
                    var template = Handlebars.compile(source);

                    var menuIndex = getAurlparam('h') || 0;
                    var thirdMenusIndex = getAurlparam('t') || 0;
                    var leftIndex = getAurlparam('i') || 0;

                    var storageContent = JSON.parse(localStorage.getItem('content'));

                    var obj = {
                        content: list,
                        fourthMenus: storageContent.menus[menuIndex].children[leftIndex].children[thirdMenusIndex].children || {}
                    }

                    var html = template(obj);
                    $('#b_content').html(html);

                    var source = $("#special_fourth_tpl").html()
                    var template = Handlebars.compile(source)
                    var html = template(storageContent.menus[menuIndex].children[leftIndex].children[thirdMenusIndex].children)
                    $(".add_template_btn").remove();
                    $('.public-botPage-left').append(html)

                    if (list.content) {
                        $('.result').html(list.content.total ? list.content.total : '0');
                    }

                    $('.all-page-container').createPager({
                        current: docListData.pageNum,
                        total: list.content.total,
                        pageSize: list.content.pageSize,
                        needInput: false,
                        backFn: function (current) {
                            docListData.pageNum = current;
                            docList();
                        }
                    })
                    Public.record('t_doc')
                }

                onBindFun();
            }
        });

    }

    docList();

    function onBindFun() {



        // 添加模板点击
        $(".add_template_btn").on('click', function () {
            pjaxGo("application/document_template/add_template.html")
        });

        $(".doc_templateSelect").on('change', function () {
            var obj = $(this).find("option:selected");
            id = $(this).data('id');
            if (obj.val() == 'downloadDoc') {
                site.send({
                    url: url.downloadDocByIdService,
                    data:{
                        ids:id
                    },
                    callback:function(data){
                        data = JSON.parse(data);
                        if(data.type == '1'){
                            window.open(data.content.zipUrl);
                        }else{
                            Constant.fn.Message("F",data.msg);
                        }
                    }
                })
            }
            if (obj.val() == 'updateDoc') {
                pjaxGo("application/document_template/add_template.html?id=" + id)
            }
            if (obj.val() == 'deleteDoc') {
                delectDoc(id);
            }
            $(this).val('-1');
        })
        //拿到所有的复选框，当有复选框没选上的时候,取消全选
        $("input:checkbox[name='ch_id']").on('click', function () {
            var checkboxs = $("input:checkbox[name='ch_id']");
            var sum = 0;
            var length = checkboxs.length;
            $.each(checkboxs, function (i, checkbox) {
                if ($(checkbox).prop("checked")) {
                    sum++;
                }
            })
            if (sum == length) {
                $('#checkboxAll').prop("checked", true);
            } else {
                $('#checkboxAll').prop("checked", false);
            }
        })
        //全选和全不选
        $('#checkboxAll').on('click', function () {
            $("input[name='ch_id']").prop("checked", $(this).prop("checked"));
        })

        // 删除
        function delectDoc(id) {
            site.send({
                url: url.delectDoc,
                //async:false
                data: {
                    ids: id
                },
                callback: function (data) {
                    var rs = JSON.parse(data);
                    if (rs.type == 1) {
                        Constant.fn.Message('T', rs.msg);
                        pjaxReload();
                    } else {
                        Constant.fn.Message('F', rs.msg);
                    }
                }
            })
        }

    }

    $("#sousuo").click(function () {
        if ($(".department_select").val() != '') {
            docListData.dptId = $(".department_select").val();
        }
        if ($("#inputNames").val() != '' && $("#inputNames").val().trim()) {
            docListData.name = $("#inputNames").val();
        }
        if ($(".doc_type").val() != '') {
            docListData.doc_type = $(".doc_type").val();
        }
        if ($("#product_choose_list").val()) {
            docListData.product_id = $("#product_choose_list").val()
        }
        docList();
    })

    $("#bulk_operation").on('change', function () {
        var data = '';
        //console.log(1);
        var selected = $(this).find("option:selected");
        $(this).find("option:first").prop("selected", false).prop("selected", true);
        $("input[name='ch_id']:checked").each(function (index, item) {
            data = data + $(item).val() + ","
        })
        data = data.substring(0, data.length - 1)
        if (selected.val() == '删除所选') {
            //删除产品
            if (!data) {
                Constant.fn.Message('I', "未选择要删除的文档")
                return;
            }
            $('.delete_winBox').toggle();

            $('.sureDel').on('click', function () {
                site.send({
                    url: url.deleteDocByIdService,
                    data: {
                        ids: data
                    },
                    callback: function (data) {
                        data = JSON.parse(data)
                        if (data.type == 1) {
                            $('.delete_winBox').toggle();
                            Constant.fn.Message('T', data.msg);
                            pjaxReload();
                        } else {
                            Constant.fn.Message("F", data.msg)
                        }
                    },
                    callError: function () {

                    }
                })
            })
        }
        if (selected.val() == '批量下载') {
            //删除产品
            if (!data) {
                Constant.fn.Message('I', "未选择要下载的文档")
                return;
            }
            //$('.delete_winBox').toggle();

            //$('.sureDel').on('click', function () {
                site.send({
                    url: url.downloadDocByIdService,
                    data: {
                        ids: data
                    },
                    callback: function (data) {
                        data = JSON.parse(data)
                        if (data.type == 1) {
                            //$('.delete_winBox').toggle();
                            window.open(data.content.zipUrl);
                            Constant.fn.Message('T', data.msg);
                            //location.reload();
                        } else {
                            Constant.fn.Message("F", data.msg)
                        }
                    },
                    callError: function () {

                    }
                })
            //})
        }
    })

})